home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / tex / ascutils.zip / TRIM.ASM < prev    next >
Assembly Source File  |  1988-05-19  |  3KB  |  124 lines

  1. ;
  2. ;                  TRIM
  3. ; This program trims the blanks    and tabs (white    space) from the    ends of    lines.
  4. ; Standard Input and Output are    used, and the DOS re-direction facility    is
  5. ; used to operate on disk files.  Lines    may be up to 255 chars long, counting
  6. ; eoln.
  7. ;
  8.  
  9.  
  10. codeseg        segment
  11.         assume    cs:codeseg, ds:codeseg
  12.  
  13.  
  14.         org    100h
  15.  
  16. trim        proc    far
  17.  
  18. start:        jmp    Main
  19.  
  20. in_handle    dw    0        ; handle for std input
  21. out_handle    dw    1        ; handle for std output
  22. buffer        db    0        ; I/O buffer, 255 chars
  23.         db    255 dup    (0)
  24.  
  25. Main:
  26.         call    get_line    ; get a    line from std input
  27.         jc    Exit        ; exit on error    (or eof)
  28.         call    trim_line    ; trim spaces/tabs from    end
  29.         call    put_line    ; send trimmed line to std output
  30.         jc    Exit        ; exit on error
  31.         jmp    short Main    ; continue until eof or    error
  32.  
  33. Exit:
  34.         mov    bx,in_handle    ; 'close' std input to flush buffers
  35.         mov    ah,3Eh
  36.         int    21h
  37.         int    20h        ; terminate program.
  38.  
  39. trim        endp
  40.  
  41. ;-------------------
  42.  
  43. ;        SUBROUTINE
  44. ;        gets line from std input
  45.  
  46. get_line    proc    near
  47.         mov    si,offset buffer    ; point    si to start of buffer
  48. get_1:
  49.         mov    dx,si            ; set up dx
  50.         mov    cx,1            ; set char count
  51.         mov    bx,in_handle        ; handle for std in (0)
  52.         mov    ah,3Fh            ; input    cx chars from std in
  53.         int    21h
  54.         jc    get_finish        ; error    - return cf set
  55.         cmp    byte ptr [si],0Ah    ; no error - chk for eoln
  56.         je    get_finish        ;  ret cf 0 if true
  57.         mov    cx,ax            ; get char count into cx
  58.         cmp    byte ptr [si],1Ah    ; check    for eof
  59.         stc                ; set cf for eof true
  60.         jz    get_finish        ; ret cf 1 if eof
  61.         jcxz    get_finish        ; ret cf 1 if 0    chars read
  62.         inc    si            ; move buffer pointer
  63.         cmp    si,offset buffer+255    ; check    for buffer overrun
  64.         stc
  65.         je    get_finish        ; error    exit if    overrun
  66.         clc
  67.         jmp    short get_1        ; get next char
  68. get_finish:    ret
  69.  
  70. get_line    endp
  71.  
  72. ;-------------------
  73.  
  74. ;        SUBROUTINE
  75. ;        this is    the routine that removes trailing blanks/ tabs.
  76.  
  77. trim_line    proc    near
  78.         sub    si,2            ; move si to precede 0D    0A
  79. trim_1:
  80.         cmp    byte ptr [si],20h    ; check    for space
  81.         je    trim_ck
  82.         cmp    byte ptr [si],9        ; check    for horiz tab
  83.         je    trim_ck
  84.         inc    si            ; keep any other char
  85.         jmp    short trim_done
  86. trim_ck:
  87.         cmp    si,offset buffer    ; at beginning of buffer?
  88.         je    trim_done        ;  if so, no more chars.
  89.         dec    si            ; point    to next    char down
  90.         jmp    short trim_1        ; repeat checks
  91. trim_done:
  92.         mov    byte ptr [si],0Dh    ; place    eoln chars here
  93.         mov    byte ptr [si+1],0Ah
  94.         ret
  95.  
  96. trim_line    endp
  97.  
  98. ;-------------------
  99.  
  100. ;        SUBROUTINE
  101. ;        output line to std output
  102.  
  103. put_line    proc    near
  104.         mov    si,offset buffer    ; point    to start of buffer
  105.         mov    dx,si            ; set up dx
  106.         xor    cx,cx            ; init char count to 0
  107. put_1:
  108.         inc    cx            ; start    counting chars
  109.         lodsb                ; mov 1    by 1 into al
  110.         cmp    al,0Ah            ; check    for eoln
  111.         jne    put_1            ; continue.
  112.         mov    bx,out_handle        ; std out handle
  113.         mov    ah,40h
  114.         int    21h            ; send to std out
  115.         ret                ; cf = 1 if error
  116.  
  117. put_line    endp
  118.  
  119. ;-------------------
  120.  
  121. codeseg        ends
  122.  
  123.         end    start
  124.